<?php

class RequestAQoute {

    const TABLENAME = 'travel_customer_request';
    /*
     * CONST OF type_request
     */
    const REQUEST_A_QUOTE = 1;
    const REQUEST_A_DEAL = 2;
    const REQUEST_A_FLIGHT = 3;
    const NEW_CUSTOMER = 4;
    const GROUP_TRAVEL = 5;

    /*
     * Admin paged
     */

    /*
     * List of travel group
     * @author:Tham
     * @param: $limt int[]
     * @param :$start int[]
     * @return $listTravelGroup array[]
     */

    public static function listRequestAQoute($start, $limt) {


        global $wpdb;

        $sql_query = 'SELECT * FROM ' . self::TABLENAME . ' '
                . '  ORDER BY  `id_request_a_quote` DESC '
                . '  LIMIT  ' . $start . ' , ' . $limt;

        $listTravelGroup = $wpdb->get_results($sql_query);

        return $listTravelGroup;
    }

    /*
     * count all of travel group
     * @author:Tham
     * @return $listTravelGroup array[]
     */

    public static function countListRequestAQoute() {


        global $wpdb;

        $sql_query = 'SELECT count(id_request_a_quote) as  totaltravel  FROM ' . self::TABLENAME;

        $listTravelGroup = $wpdb->get_results($sql_query);

        return $listTravelGroup;
    }

    /*
     * Insert Data Base on travel group add
     * @author:tham
     * $param :$groupName,$MemberGroup,$startLocation,$endLocation	,$adult	,$children,$class
     * @return none
     */

    public static function insertRequestAQoute($typeFlights,$fullName, 
	    $email,$phone,$startLocation,$endLocation,$startTime,$endTime,$adult,$children,$infant,$class,
	    $city,$typeRequest,$price = 0, $address = '') {
         global $wpdb;
          $startLocation = serialize($startLocation);
          $endLocation   = serialize($endLocation);
          $startTime     = serialize($startTime);
          $endTime       = serialize($endTime);
          
          $sql_insert = "INSERT INTO " . self::TABLENAME . " (type_flights,full_name,email,phone,start_location,
	      end_location,start_time,end_time,adult,children,infant,class, city, type_request, price, address)"
                        . "  VALUES (".$typeFlights.",'".$fullName."','".$email."','".$phone."',
			    '".$startLocation."','".$endLocation."','".$startTime."','".$endTime."',".$adult.",
				".$children.",".$infant.",".$class.",'".$city."',".$typeRequest.",'".$price."',
				    '".$address."')";
                  
          $wpdb->query($sql_insert);
    }

    /*
     * select travel group by $idRequestAquote
     * @author:Tham
     * @param idTravelGroupTour;
     * @return $listTravelGroup array[]
     */

    public static function listRequestAqouteByIdRequestAquote($idRequestAquote) {


        global $wpdb;

        $sql_query = 'SELECT  *  '
                . ' FROM ' . self::TABLENAME . " "
                . " WHERE  id_request_a_quote = " . $idRequestAquote;

        $listTravelGroup = $wpdb->get_results($sql_query);
        //echo $listTravelGroup;

        return $listTravelGroup;
    }

    /*
     * Insert Data Base on Request A qoute
     * @author:tham
     * $param :$groupName,$MemberGroup,$startLocation,$endLocation	,$adult	,$children,$class
     * @return none
     */

    public static function updateRequestAqoute($idRequestAQuote, $typeFlights, $startLocation, $endLocation, $startTime, $endTime, $adult, $children, $infant, $class, $city) {
        global $wpdb;

        if ($typeFlights == DealFlight::MULTICITY) {

            //  echo $startTime2.$endTime2;
            //   echo $startTime3.$endTime3;
            // echo $startTime4.$endTime4;

            $sql_update = '    UPDATE ' . self::TABLENAME . '  '
                    . '    SET    type_flights = "' . $typeFlights . '",'
                    . '           start_location  = "" ,'
                    . '           end_location = "",'
                    . '           start_time = "",'
                    . '           end_time = "",'
                    . '           start_location1  = "' . $startLocation[0] . '" ,'
                    . '           end_location1 = "' . $endLocation[0] . '",'
                    . '           start_time1 = "' . $startTime1 . '",'
                    . '           end_time1 = "' . $endTime1 . '",'
                    . '           start_location2  = "' . $startLocation[1] . '" ,'
                    . '           end_location2 = "' . $endLocation[1] . '",'
                    . '           start_time2 = "' . $startTime2 . '",'
                    . '           end_time2 = "' . $endTime2 . '",'
                    . '           start_location3  = "' . $startLocation[2] . '" ,'
                    . '           end_location3 = "' . $endLocation[2] . '",'
                    . '           start_time3 = "' . $startTime3 . '",'
                    . '           end_time3 = "' . $endTime3 . '",'
                    . '           start_location4  = "' . $startLocation[3] . '" ,'
                    . '           end_location4 = "' . $endLocation[3] . '",'
                    . '           start_time4 = "' . $startTime4 . '",'
                    . '           end_time4 = "' . $endTime4 . '",'
                    . '           adult = ' . $adult . ','
                    . '           children = ' . $children . ','
                    . '           infant = ' . $infant . ','
                    . '           class = ' . $class . ','
                    . '           city = "' . $class . '"'
                    . '    WHERE  id_request_a_quote = ' . $idRequestAQuote;
            $wpdb->query($sql_update);
        } else {
            $dsstartTime = explode(' ', $startTime);
            $house = $dsstartTime[1];
            $day = $dsstartTime[0];
            $dsDay = explode('/', $day);
            $day = $dsDay[2] . '-' . $dsDay[1] . '-' . $dsDay[0];

            $startTime = $day . ' ' . $house;

            $dsendTime = explode(' ', $endTime);
            $house = $dsendTime[1];
            $day = $dsendTime[0];
            $dsDay = explode('/', $day);
            $day = $dsDay[2] . '-' . $dsDay[1] . '-' . $dsDay[0];

            $endTime = $day . ' ' . $house;

            $sql_update = '    UPDATE ' . self::TABLENAME . '  '
                    . '    SET    type_flights = "' . $typeFlights . '",'
                    . '           start_location  = "' . $startLocation . '" ,'
                    . '           end_location = "' . $endLocation . '",'
                    . '           start_time = "' . $startTime . '",'
                    . '           end_time = "' . $endTime . '",'
                    . '           start_location1  = "" ,'
                    . '           end_location1 = "",'
                    . '           start_time1 = "",'
                    . '           end_time1 = "",'
                    . '           start_location2  = "" ,'
                    . '           end_location2 = "",'
                    . '           start_time2 = "",'
                    . '           end_time2 = "",'
                    . '           start_location3  = "" ,'
                    . '           end_location3 = "",'
                    . '           start_time3 = "",'
                    . '           end_time3 = "",'
                    . '           start_location4  = "" ,'
                    . '           end_location4 = "",'
                    . '           start_time4 = "",'
                    . '           end_time4 = "",'
                    . '           adult = ' . $adult . ','
                    . '           children = ' . $children . ','
                    . '           infant = ' . $infant . ','
                    . '           class = ' . $class . ','
                    . '           city = "' . $class . '"'
                    . '    WHERE  id_request_a_quote = ' . $idRequestAQuote;
            // echo $sql_update;
            $wpdb->query($sql_update);
        }
    }

    public static function delete($id) {
        global $wpdb;
        $sql_delete = "DELETE FROM `wp_request_a_quote` WHERE `id_request_a_quote` = " . $id;
        $wpdb->query($sql_delete);
    }

    /*
     * More add database of Admin
     * 
     */

    public static function insertRequestAQuoteAdmin($fullName, $email, $address, $phone) {
        global $wpdb;

        $sql_insert = "INSERT INTO " . self::TABLENAME . " (full_name,email,address,phone,type_request) "
                . "    VALUES('" . $fullName . "','" . $email . "','" . $address . "','" . $phone . "','" . self::NEW_CUSTOMER . "')";

        $wpdb->query($sql_insert);
    }
    
    

    /*
     * Get List customer of request a quote by idRequestAQuote
     * @param  idRequestAQuote
     * @return $listCustomer
     */

    public static function ListCustomerbyIdRequestAQuote($idRequestAQuote) {
        
        global $wpdb;
        $sql_query = "SELECT * FROM `travel_customer_request` WHERE `id_request_a_quote`=".$idRequestAQuote;
        $listCustomer = $wpdb->get_results($sql_query);
        //echo $listTravelGroup;

        return $listCustomer;
    }
    
    public static function updateRequestCustomerGenneral($idRequestAQuote,$fullName, $email,$address, $phone,$startLocation,$endLocation,$startTime,$endTime,$adult,$children,$infant,$class,$city,$price){
        
          global $wpdb;
  
          $startLocation = serialize($startLocation);
          $endLocation   = serialize($endLocation);
          $startTime     = serialize($startTime);
          $endTime       = serialize($endTime);
          
         $sql_update = "   UPDATE " . self::TABLENAME . "  "
                    . "    SET    full_name ='".$fullName."' ,"
                    . "           email  = '" . $email . "' ,"
                    . "           address  = '" . $address . "' ,"
                    . "           phone  = '" . $phone . "' ,"
                    . "           start_location  = '" . $startLocation . "' ,"
                    . "           end_location = '" . $endLocation . "',"
                    . "           start_time = '" . $startTime . "',"
                    . "           end_time = '" . $endTime . "',"
                    . "           adult = " . $adult . ","
                    . "           children = " . $children . ","
                    . "           infant = " . $infant . ","
                    . "           class  = " . $class . ","
                    . "           city   = '" . $city . "',"
                    . "           price  = '".$price."'"
                    . "   WHERE  id_request_a_quote = " . $idRequestAQuote;           
          $wpdb->query($sql_update);
       
    }
    /**
     * get latest requesting
     * @par
     */
    public static function getLatestQuote($typeRequest){
	global $wpdb;
	$sql = 'SELECT * FROM '.self::TABLENAME.' WHERE `type_request`='.$typeRequest.' 
	    ORDER BY `id_request_a_quote` DESC LIMIT 5';
	$data = $wpdb->get_results($sql);
	return $data;
    }

}
